//
//  XSVoiceDemo.m
//  reading
//
//  Created by xingshuai on 2020/10/9.
//

#import "XSVoiceDemo.h"

#import <AVFoundation/AVFoundation.h>

@interface XSVoiceDemo () <AVSpeechSynthesizerDelegate>

@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer; // 语音合成器，说话的人

@end

@implementation XSVoiceDemo

+ (XSVoiceDemo *)shareInstance {
    static XSVoiceDemo *voDemo = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        voDemo = [[XSVoiceDemo alloc] init];
        voDemo.synthesizer = [[AVSpeechSynthesizer alloc] init];
    });
    return voDemo;
}

/// 播放
/// @param works 要朗读的声音
- (void)speakWithWorks:(NSString *)works {
    AVSpeechUtterance *uttss = [[AVSpeechUtterance alloc] initWithString:works];
    uttss.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    [self.synthesizer speakUtterance:uttss];
}


//开始朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
    NSLog(@"didStartSpeechUtterance->%@",utterance.speechString);
}
//结束朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
    NSLog(@"didFinishSpeechUtterance->%@",utterance.speechString);
}
//暂停朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{
    NSLog(@"didPauseSpeechUtterance->%@",utterance.speechString);
}
//继续朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{
    NSLog(@"didContinueSpeechUtterance->%@",utterance.speechString);
}
//取消朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{
    NSLog(@"didCancelSpeechUtterance->%@",utterance.speechString);
}
//将要播放的语音文字
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{
    NSLog(@"willSpeakRangeOfSpeechString->characterRange.location = %zd->characterRange.length = %zd->utterance.speechString= %@",characterRange.location,characterRange.length,utterance.speechString);
    //self.speechLabel.text = utterance.speechString;
}


@end
